package com.bootdo.wx.service.impl;

import com.bootdo.common.utils.IPUtils;
import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.common.utils.excel.ExtractExcelUtils;
import com.bootdo.common.utils.excel.OrderExcelUtils;
import com.bootdo.wx.dao.CmsContentDao;
import com.bootdo.wx.dao.MemberAuthDao;
import com.bootdo.wx.dao.MemberDoctorDao;
import com.bootdo.wx.dao.MemberPriceDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.CmsContentDO;
import com.bootdo.wx.domain.MemberAuthDO;
import com.bootdo.wx.domain.MemberDoctorDO;
import com.bootdo.wx.domain.MemberPriceDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.OrderComService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import com.bootdo.wx.dao.MemberExtractDao;
import com.bootdo.wx.domain.MemberExtractDO;
import com.bootdo.wx.service.MemberExtractService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

import static com.bootdo.common.utils.ShiroUtils.getUserId;


@Service
public class MemberExtractServiceImpl implements MemberExtractService {
	@Autowired
	private MemberExtractDao memberExtractDao;
	@Autowired
	private CmsContentDao cmsContentDao;
	@Autowired
	private OrderComService orderComService;
	@Autowired
	private MemberPriceDao memberPriceDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private MemberDoctorDao memberDoctorDao;
	@Autowired
	private MemberAuthDao memberAuthDao;
	
	@Override
	public MemberExtractDO get(String id){
		return memberExtractDao.get(id);
	}
	
	@Override
	public List<MemberExtractDO> list(Map<String, Object> map){
		return memberExtractDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberExtractDao.count(map);
	}
	
	@Override
	public int save(MemberExtractDO memberExtract){
		return memberExtractDao.save(memberExtract);
	}
	
	@Override
	public int update(MemberExtractDO memberExtract){
		return memberExtractDao.update(memberExtract);
	}
	
	@Override
	public int remove(String id){
		return memberExtractDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberExtractDao.batchRemove(ids);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public PageInfo<MemberExtractDO> getExtractList(Map<String, Object> map) {
		PageHelperUtils.page(map);
		List<MemberExtractDO> list = memberExtractDao.list(map);
		PageInfo<MemberExtractDO> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public R goExtract(Map<String, Object> map) {
		//查看用户有没有认证
		String userId = map.get("userId").toString();
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		if(memberUserDO.getLevel()==3){
			MemberDoctorDO findByUserId = memberDoctorDao.findByUserId(userId);
			if(findByUserId==null||findByUserId.getStatus()!=1){
				return R.error(101, "必须认证后或者认证通过后才能提现");
			}
		}else if(memberUserDO.getLevel()<3){
			MemberAuthDO findByUserId = memberAuthDao.findByUserId(userId);
			if(findByUserId==null||findByUserId.getStatus()!=1){
				return R.error(101, "必须认证后或者认证通过后才能提现");
			}
		}
		String type = map.get("type").toString();
		String code = map.get("code").toString();
		BigDecimal money = new BigDecimal(map.get("money").toString()); //提现金额
		map.clear();
		map.put("type", "extract-Money");
		CmsContentDO cmsContentDo = cmsContentDao.list(map).get(0);
		BigDecimal minMoney = new BigDecimal(cmsContentDo.getValue());   //提现最低金额
		map.clear();
		map.put("userId",userId);
		Map<String,Object> moneyMap = orderComService.getMemberCom(map);
		BigDecimal haveMoney = new BigDecimal(moneyMap.get("canExPrice").toString());   //可提现余额
		if (money.compareTo(minMoney) == -1) {
			return R.error("提现金额最小为 "+minMoney);
		}
		if (money.compareTo(haveMoney) == 1) {
			return R.error("您的余额不足");
		}

		MemberExtractDO extractDO = new MemberExtractDO();
		Date date = new Date();
		extractDO.setId(UuidUtil.get32UUID());
		extractDO.setDelFlag(0);
		extractDO.setCreateAt(date);
		extractDO.setUpdateAt(date);
		extractDO.setStatus(0);
		extractDO.setUserId(userId);
		extractDO.setType(type);
		extractDO.setCode(code);
		extractDO.setMoney(money);
		memberExtractDao.save(extractDO);
		//填入会员余额表
		MemberPriceDO priceDO = new MemberPriceDO();
		priceDO.setId(UuidUtil.get32UUID());
		priceDO.setGId(extractDO.getId());
		priceDO.setType(1);
		priceDO.setDelFlag(0);
		priceDO.setCreateAt(date);
		priceDO.setUpdateAt(date);
		priceDO.setUserId(userId);
		priceDO.setPrice(money);
		memberPriceDao.save(priceDO);

		return R.ok();
	}

	@Override
	public List<Map<String,Object>> getList(Map<String, Object> map) {
		return memberExtractDao.getList(map);
	}

	@Override
	public int getListCount(Map<String, Object> map) {
		return memberExtractDao.getListCount(map);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R shen(MemberExtractDO memberExtractDO) {
		String id = memberExtractDO.getId();
		MemberExtractDO extractDO = memberExtractDao.get(id);
		if (extractDO.getStatus() != 0) {
			return R.error("已经提现过了，请刷新页面");
		}
		memberExtractDO.setUpdateAt(new Date());
		memberExtractDao.update(memberExtractDO);
		if (memberExtractDO.getStatus() == 2) {
			//如果拒绝提现，修改我的余额记录
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("gId",memberExtractDO.getId());
			map.put("type", "1");
			MemberPriceDO priceDO = memberPriceDao.list(map).get(0);
			MemberPriceDO memberPriceDO = new MemberPriceDO();
			memberPriceDO.setId(priceDO.getId());
			memberPriceDO.setUpdateAt(new Date());
			memberPriceDO.setDelFlag(1);
			memberPriceDao.update(memberPriceDO);
		}
		return R.ok();
	}

	/*导出excel。*/
	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public void exportExcel(List<Map<String,Object>> userList,
							ServletOutputStream out,HttpServletRequest request) {
		ExtractExcelUtils.exportUserExcel(userList, out);
	}

	@Override
	public BigDecimal getNumByUserT(Map<String, Object> map) {
		return memberExtractDao.getNumByUserT(map);
	}
	
}
